using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using log4net;
using log4net.Core;

namespace Quiki
{
    public class ConsoleLogger : ILogger
    {
        public void Log(Level level, string messageFormat, params object[] args)
        {
            StringBuilder message = new StringBuilder();

            if (level > Level.Info)
                message.AppendFormat("{0}: ", level.ToString().ToUpper(CultureInfo.InvariantCulture));

            foreach (string value in contextValues)
                message.AppendFormat("[{0}]", value);

            message.Append(' ');
            message.AppendFormat(
                CultureInfo.InvariantCulture,
                messageFormat,
                args);
            LoggingEventData data = new LoggingEventData()
                                        {
                                            Level = level, 
                                            Message = message.ToString(),
                                            LoggerName = log.Logger.Name,
                                            TimeStamp = DateTime.Now,
                                        };

            log.Logger.Log(new LoggingEvent(data));
        }

        public void PopContext()
        {
            contextValues.RemoveAt(contextValues.Count - 1);
        }

        public void PushContext(string value)
        {
            contextValues.Add(value);
        }

        private List<string> contextValues = new List<string> ();
        private static readonly ILog log = LogManager.GetLogger(typeof(ConsoleLogger));
    }
}